package org.boot.ssm.wxshop.web.controller;

import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.Goods;
import org.boot.ssm.wxshop.domain.GoodsTypeSmall;
import org.boot.ssm.wxshop.domain.ShopCar;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.service.GoodsService;
import org.boot.ssm.wxshop.service.GoodsTypeSmallService;
import org.boot.ssm.wxshop.service.ShopCarService;
import org.boot.ssm.wxshop.service.SpecificationsService;
import org.boot.ssm.wxshop.util.WebConstant;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: wxshop
 * @description: 商品控制器
 * @author: Parasomnia
 * @create: 2018-03-15 10:49
 **/
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeSmallService goodsTypeSmallService;

    @Autowired
    private SpecificationsService specificationsService;

    @Autowired
    private ShopCarService shopCarService;

    /**
     * 根据条件搜索并分页
     * @param goods
     * @param pageNo
     * @param orderBy
     * @param order
     * @return
     */
    @RequestMapping("/front/loadGoodsData")
    @ResponseBody
    public JsonResult loadGoodsData(Goods goods,@RequestParam(defaultValue = "1") Integer pageNo,String orderBy,Boolean order){
        PageInfo<Goods> pageInfo = goodsService.queryPageListByParamPlus(pageNo, WebConstant.PAGESIZE,goods,orderBy,order? 1:0);
        return new JsonResult(0,"获取成功",pageInfo);
    }


    /**
     * 到商品列表页面
     * @param goods
     * @param model
     * @return
     */
    @RequestMapping("/front/goProList")
    public String goProList(Goods goods,Model model,@RequestParam(defaultValue = "es_goods.createTime") String orderBy){
        goods.setIsDelete(1);
        model.addAttribute("good",goods);
        model.addAttribute("orderBy",orderBy);
        model.addAttribute("goodsPage",goodsService.queryPageListByParamPlus(1,WebConstant.PAGESIZE,goods,orderBy,0));
        return "frontstage/goods/pro_list";
    }

    /**
     * 查看商品详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/front/detail/{id}")
    public String goodsDetail(@PathVariable("id") Integer id, Model model, HttpSession session){
        Goods goods = goodsService.queryByIdPlus(id);
        model.addAttribute("goods",goods);
        //商品轮播
        model.addAttribute("imgs",goods.getCarouselImgs().split(","));
        //参数选择
        model.addAttribute("specs",specificationsService.queryAllList());
        Object obj = session.getAttribute("user");
        if(obj!=null){
            User u = (User)obj;
            ShopCar car = new ShopCar();
            car.setIsDelete(1);
            car.setUserId(u.getId());
            model.addAttribute("carNum",shopCarService.queryCount(car));
        }
        return "frontstage/goods/pro_info";
    }

    /**
     * 首页的销售列表
     * @return
     */
    @RequestMapping("/front/saleList")
    @ResponseBody
    public JsonResult saleList(){
        return new JsonResult(0,"获取成功",getPageByCondition(12,"salenum"));
    }

    /**
     * 首页的收藏列表
     * @return
     */
    @RequestMapping("/front/collList")
    @ResponseBody
    public JsonResult collList(){
        return new JsonResult(0,"获取成功",getPageByCondition(12,"collectionnum"));
    }


    /**
     * 首页的推荐列表
     * @return
     */
    @RequestMapping("/front/recommendList")
    @ResponseBody
    public JsonResult recommendList(){
        return new JsonResult(0,"获取成功",getPageByCondition(12,"createtime"));
    }

    /**
     * 获取页面的部分内容
     * @param size
     * @param condition
     * @return
     */
    private List<Goods> getPageByCondition(Integer size,String condition){
        Goods goods = new Goods();
        goods.setIsDelete(1);
        return goodsService.queryPageListByParamPlus(1,size,goods,condition,0).getList();
    }





//=======================后台===========================================================================================



    /**
     * 去往商品界面
     * @param model request
     * @return
     */
    @RequestMapping("/goGoodsmgr")
    public String goodsMgr(Model model){
        GoodsTypeSmall goodsTypeSmall = new GoodsTypeSmall();
        goodsTypeSmall.setIsDelete(1);
        //获取所有商品子类型
        model.addAttribute("typeList",goodsTypeSmallService.queryParamList(goodsTypeSmall));
        return "backstage/goods/mgr";
    }

    /**
     * 查看商品详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/view")
    public String goodsDetail(Model model,Integer id){
        //根据id查询商品
        Goods goods = goodsService.queryByIdPlus(id);
        //将轮播字符串变为数组
        String temp = goods.getCarouselImgs();
        if(temp!=null) {
            String[] imgs = temp.split(",");
            model.addAttribute("imgs",imgs);
        }
        model.addAttribute("goods",goods);
        return "backstage/goods/view";
    }



    /**
     * 加载商品信息并分页
     * @param page
     * @param limit
     * @param goods
     * @return
     */
    @RequestMapping("/goodsList")
    @ResponseBody
    public JsonResult goodsList(Integer page, Integer limit, Goods goods){
        PageInfo<Goods> pageInfo = goodsService.queryPageListByParamPlus(page,limit,goods,null,null);
        return new JsonResult(0,"加载成功",pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 商品批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteMany")
    @ResponseBody
    public JsonResult deleteMany(@RequestBody List<Integer> ids){
        goodsService.updateDeleteMany(ids);
        return new JsonResult(0,"批量删除成功");
    }

    /**
     * 单条删除
     * @param goods
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Goods goods){
        goodsService.updateDelete(goods);
        return new JsonResult(0,"删除成功");
    }

    @RequestMapping("/goaddoredit")
    public String goAddOrEdit(Integer id,Model model){
        if(id!=null){
            Goods goods = goodsService.queryByIdPlus(id);
            model.addAttribute("goods",goods);
        }
        GoodsTypeSmall goodsTypeSmall = new GoodsTypeSmall();
        goodsTypeSmall.setIsDelete(1);
        model.addAttribute("typeList",goodsTypeSmallService.queryParamList(goodsTypeSmall));
        return "backstage/goods/addoredit";
    }

    /**
     * 添加或修改商品
     * @param goods
     * @param carouselImgs
     * @return
     */
    @RequestMapping("/addOrEdit")
    @ResponseBody
    public JsonResult addOrEdit(Goods goods,@RequestParam(required = false,value = "carouselImgs") List<String> carouselImgs){
        StringBuffer temp = new StringBuffer();
        if(carouselImgs!=null){
            for(int a = 0;a<carouselImgs.size();a++){
                temp.append(carouselImgs.get(a));
                if(a<carouselImgs.size()-1)
                    temp.append(",");
            }
        }
        goods.setCarouselImgs(temp.toString());
        goodsService.addOrEdit(goods);
        return new JsonResult(200,"操作成功");
    }

}
